//
//  JTOptionBar.m
//

#import "JTOptionBar.h"

//---------------------------------------------------------------------
// JTOption
//---------------------------------------------------------------------
@implementation JTOption

+(JTOption*)optionWithIndex:(NSInteger)index value:(id)value title:(NSString *)title action:(JTOptionBarAction)action
{
    JTOption* option=[[JTOption alloc] initWithIndex:index value:value title:title action:action];
    return option;
}

-(instancetype)initWithIndex:(NSInteger)index value:(id)value title:(NSString*)title action:(JTOptionBarAction)action
{
    if (self=[super init]) {
        self.index=index;
        self.value=value;
        self.title=title;
        self.action=action;
    }
    return self;
}

@end
//---------------------------------------------------------------------


@interface JTOptionBar ()

@property (strong,nonatomic) UIImageView* indicator;
@property (strong,nonatomic) NSMutableArray* options;
@property (strong,nonatomic) JTOption* selectedOption;

@end

@implementation JTOptionBar
{
    NSInteger _count;
    CGFloat _width;
    CGFloat _height;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self=[super initWithFrame:frame]) {
        self.options=[[NSMutableArray alloc] init];
    }
    return self;
}

-(void)addOptionWithValue:(id)value title:(NSString *)title action:(JTOptionBarAction)action
{
    NSInteger index=self.options.count;
    [self.options addObject:[JTOption optionWithIndex:index value:value title:title action:action]];
    if (index==0) {
        self.selectedOption=self.options.firstObject;
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    _count=self.options.count;
    _width=self.bounds.size.width/_count;
    _height=self.bounds.size.height;
    self.backgroundColor=self.config.backgroundColor;
    for (JTOption* option in self.options) {
        UIButton* button=[UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:option.title forState:UIControlStateNormal];
        [button.titleLabel setFont:self.config.font];
        [button setTitleColor:self.config.textColor forState:UIControlStateNormal];
        button.frame=CGRectMake(option.index*_width, 0, _width, _height);
        button.tag=option.index;
        [self addSubview:button];
        [button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    self.indicator=[[UIImageView alloc] initWithImage:self.config.indicatorImage];
    self.indicator.frame=CGRectMake(self.selectedOption.index*_width, _height-5, _width, 5);
    [self addSubview:self.indicator];
}

-(void)buttonPressed:(UIButton*)sender
{
    if (sender.tag!=self.selectedOption.index) {
        self.selectedOption=self.options[sender.tag];
        CGFloat centerX;
        centerX=_width*sender.tag+_width/2;
        [UIView animateWithDuration:0.4 animations:^{
            self.indicator.frame=CGRectMake(sender.tag*_width, _height-5, _width, 5);
        }];
    }
    JTOption* option=self.options[sender.tag];
    option.action(self);
}

@end
